var mailer = require('nodemailer'),
    crypto = require('crypto'),
    uuid = require('node-uuid');

var SMTP_CONFS = [
    {
        ssid: "m000",
        host: "smtp.yeah.net",
        port: 25,
        name: "uolanoreply",
        maxConnections: 3,
        auth: {
            user: "uuolanoreply@yeah.net",
            pass: "hongdeviltxd0321"
        }
    }
    ,
    {
        ssid: "m001",
        host: "smtp.163.com",
        port: 25,
        name: "txdnetmailer",
        maxConnections: 3,
        auth: {
            user: "txdnetmailer@163.com",
            pass: "MdFM038iDF"
        }
    }
    ,
    {
        ssid: "m002",
        host: "smtp.sina.com",
        port: 25,
        name: "taobaogouwu_kit",
        maxConnections: 3,
        auth: {
            user: "taobaogouwu_kit@sina.com",
            pass: "20130119uuola001"
        }
    }
    ,
    {
        ssid: "m003",
        host: "smtp.126.com",
        port: 25,
        name: "tmallgouwu_kit",
        maxConnections: 3,
        auth: {
            user: "tmallgouwu_kit@126.com",
            pass: "20130119uuola002"
        }
    }
    ,
    {
        ssid: "m004",
        host: "smtp.yeah.com",
        port: 25,
        name: "tao_uuola_com",
        maxConnections: 3,
        auth: {
            user: "tao_uuola_com@yeah.net",
            pass: "20130119uuola003"
        }
    }
    ,
    {
        ssid: "m005",
        host: "smtp.189.cn",
        port: 25,
        name: "olatao",
        maxConnections: 3,
        auth: {
            user: "olatao@189.cn",
            pass: "huse2004011947"
        }
    }
];

var TransPorts = [];
for (var k in SMTP_CONFS) {
    TransPorts.push(mailer.createTransport("SMTP", SMTP_CONFS[k]));
}
;


var Nodedm = {};

// 对外处理接口
// /nodedm/receive?
// clientEmail=abcde@gmail.com&
// actTemplate=asdfjksdfjdsfsf.edm&
// subject= admin账户重要信息&
// fromEmail=噢啦网
Nodedm.receive = function (req, res) {
    var toMail = req.query.clientEmail;
    var subject = req.query.subject;
    var fromMail = req.query.fromEmail;
    var actTemplate = req.query.actTemplate;
    var content = req.query.content;
    var rndIdx = Nodedm.getRndIndex();
    var tranSender = TransPorts[rndIdx];
    var mailEntity = Nodedm.buildMailEntity(toMail, fromMail, content, subject, rndIdx);
    tranSender.sendMail(mailEntity, function (err, response) {
        if (err) {
            res.send('fail Message sent:' + err.toString() + ' mailEntity.messageId=' + mailEntity.messageId);
        } else {
            res.send('ok');
        }
    });
};

Nodedm.getRndIndex = function () {
    var len = TransPorts.length;
    var index = parseInt(Math.random() * len);
    console.log(index);
    return index;
};

Nodedm.buildMessageId = function (extString) {
    var rndKey = uuid.v1() + uuid.v4();
    var sha1 = crypto.createHash('sha1');
    sha1.update(rndKey);
    return sha1.digest(encoding = 'hex') + "." + extString;
};

Nodedm.buildMailEntity = function (clientEmail, fromMail, content, subject, rndIdx) {
    var smtpConf = SMTP_CONFS[rndIdx];
    var MailEntity = {
        headers: {
            "X-Mailer":"Node Mailer.",
            "X-Server-By": "Node Edm Server.",
            "X-Priority":"2"
        },
        messageId: Nodedm.buildMessageId(smtpConf.ssid),
        from: fromMail + '<' + smtpConf.auth.user + '>',
        to: clientEmail,
        subject: subject,
        html: content.replace(/\$\{clientEmail\}/g, clientEmail)
    };
    return MailEntity;
};

exports.receive = Nodedm.receive;